package com.luoying.itew.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import androidx.core.content.ContextCompat;

import com.luoying.itew.common.AntaiApplication;

/**
 * Created by lufan on 2019/4/10
 * Class describe
 */
public class AppUtils {

    /**
     * 跳转app详情页面
     * @param context
     */
    public static void toSeting(Context context){
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        Uri uri = Uri.fromParts("package", context.getPackageName(), null);
        intent.setData(uri);
        context.startActivity(intent);
    }

    /**
     * 检查app是非有当前权限
     * @param args 判断的权限列表
     */
    public static boolean checkPermission(Context context,String[]args){
        boolean isGranted = Boolean.FALSE;
        for (String arg : args) {
            if(ContextCompat.checkSelfPermission(context, arg)== PackageManager.PERMISSION_GRANTED){
                isGranted = Boolean.TRUE;
            } else {
                break;
            }
        }
        return isGranted;
    }

    /**
     * 获取版本名称
     *
     * @param context 上下文
     *
     * @return 版本名称
     */
    public static String getVersionName(Context context) {

        //获取包管理器
        PackageManager pm = context.getPackageManager();
        //获取包信息
        try {
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
            //返回版本号
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int sp2px(float spValue) {
        final float fontScale = AntaiApplication.context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     *
     * @param dipValue
     *  （DisplayMetrics类中属性density）
     *
     * @return
     */
    public static int dip2px(float dipValue) {
        final float scale = AntaiApplication.context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
